package com.woan.api.idao.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.woan.api.idao.SysPhotoIDAO;
import com.woan.api.model.SysPhoto;
import com.woan.api.vo.VphotoList;


/**
 * A data access object (DAO) providing persistence and search support for
 * SysPhoto entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.woan.api.model.SysPhoto
 * @author MyEclipse Persistence Tools
 */

public class SysPhotoDAO extends HibernateDaoSupport  implements SysPhotoIDAO{
	//根据视频id获取视频对象
		public SysPhoto get(String photoid) {
			return (SysPhoto) this.getHibernateTemplate().get(SysPhoto.class,
					photoid);
		}
	//根据自定义返回的实体返回历史列表
	public List<VphotoList> FindVPhotoByGroup(String groupid, Integer beginIndex,Integer endIndex)
	{
		StringBuffer  buffer=new StringBuffer("select ph.pid,ph.url,ph.smurl,ph.creattime,ph.title,ph.province," +
				"ph.city,ph.counties,ad.userName  as \"userName\"  " +
				"from sys_photo as ph,sys_admininfo as ad where  ph.rid='"+groupid+"' and ad.uid=ph.uid " +
						"and (ph.isdel='1') order by creattime desc");
		
		//System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(VphotoList.class));
			query.setFirstResult(beginIndex);
			query.setMaxResults(endIndex);
			List<VphotoList> list=query.list();
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	//根据自定义返回的实体返回历史列表
		public List<VphotoList> FindVPhotoByUid(String uid, Integer beginIndex,Integer endIndex)
		{
			StringBuffer  buffer=new StringBuffer("select ph.pid,ph.url,ph.smurl,ph.creattime,ph.title,ph.province," +
					"ph.city,ph.counties,ad.userName as \"userName\" " +
					"from sys_photo as ph,sys_admininfo as ad where  ph.uid='"+uid+"' and ad.uid=ph.uid " +
							"and (ph.isdel='1') order by creattime desc");
			
			//System.out.println(buffer.toString());
			try
			{
				Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
						Transformers.aliasToBean(VphotoList.class));
				query.setFirstResult(beginIndex);
				query.setMaxResults(endIndex);
				List<VphotoList> list=query.list();
				
				return list;
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			return null;
		}
		//根据自定义返回的实体返回搜索列表
			public List<VphotoList> FindVSerachPhotoByCon(String groupid,Integer beginIndex,Integer endIndex,Map<String, String> map)
			{
				StringBuffer  buffer=new StringBuffer("select ph.pid,ph.url,ph.smurl,ph.creattime,ph.title,ph.province," +
						"ph.city,ph.counties,ad.userName as \"userName\" " +
						"from sys_photo as ph,sys_admininfo as ad where  ph.rid='"+groupid+"' and ad.uid=ph.uid " +
								"and (ph.isdel='1')");
				if(map.get("fabuname").toString()!=null && !map.get("fabuname").equals(""))
					buffer.append(" and ad.userName like '%"+map.get("fabuname").toString()+"%' ");
				if(map.get("title").toString()!=null && !map.get("title").equals(""))
					buffer.append(" and ph.title like '%"+map.get("title").toString()+"%' ");
				if(map.get("begintime").toString()!=null && !map.get("begintime").equals("")){
					buffer.append(" and ph.creattime >= '"+ map.get("begintime")+" 00:00:00'");
				}
				if(map.get("endtime").toString()!=null && !map.get("endtime").equals("")){
					buffer.append(" and ph.creattime <= '"+ map.get("endtime")+" 23:59:59'");
				}
				buffer.append(" order by creattime desc ");
				//System.out.println(buffer.toString());
				try
				{
					Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
							Transformers.aliasToBean(VphotoList.class));
					query.setFirstResult(beginIndex);
					query.setMaxResults(endIndex);
					List<VphotoList> list=query.list();
					
					return list;
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				return null;
			}
	//编辑视频描述
		public String EditPhotos(String photoid,String title)
		{
			SysPhoto sysphoto = this.get(photoid);
			
			String flag = "0";
			if(sysphoto!=null)
			{
				try
				{
					sysphoto.setTitle(title);
					this.getHibernateTemplate().saveOrUpdate(sysphoto);
				}
				catch(Exception e){
					e.printStackTrace();
					flag="1";
				}
			}
			return flag;
		}
		//删除视频
		public boolean DeletePhotos(String photoid)
		{
			Session session=this.getSession();
			StringBuffer hql=new StringBuffer(" update sys_photo set isdel='0' where pid='"+photoid+"' ");
			//System.out.println(hql.toString());
			session.createSQLQuery(hql.toString()).executeUpdate();
			return true;
		}
		//照片数量
				public int FindPhotosCountByGroup(String groupid)
				{
					String hql="from SysPhoto where rid='"+groupid+"' and isdel = '1'";
					try
					{
						List list=this.getHibernateTemplate().find(hql);
						if(list.size()>0)
							return list.size();
						else
							return 0;
					}
					catch(Exception e)
					{
						e.printStackTrace();
					}
					return 0;
				}
	public String save(SysPhoto photo)
	{
		String flag="1";
		try {
			getHibernateTemplate().save(photo);
			flag = "0";
		} catch (RuntimeException re) {
			throw re;
		}
		return flag;
	}
}